<?php

class Common {

    public static $ChineseNumber = array(
        0 => '零',
        1 => '一',
        2 => '二',
        3 => '三',
        4 => '四',
        5 => '五',
        6 => '六',
        7 => '七',
        8 => '八',
        9 => '九',
        10 => '十',
    );

    /**
     * 截取字符串
     * @param <type> $string 输入字符串
     * @param <type> $length 长度
     * @param <type> $dot 超过长度时后缀
     * @param <type> $charset编码
     * @return <type> 
     */
    public static function strCut($string, $length, $dot = '...', $charset = 'utf-8') {
        $strlen = strlen($string);
        if ($strlen <= $length) {
            return $string;
        }
        $strcut = '';
        if (strtolower($charset) == 'utf-8') {
            $n = $tn = $noc = 0;
            while ($n < $strlen) {
                $t = ord($string[$n]);
                if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                    $tn = 1;
                    $n++;
                    $noc++;
                } else if (194 <= $t && $t <= 223) {
                    $tn = 2;
                    $n += 2;
                    $noc += 2;
                } else if (224 <= $t && $t <= 239) {
                    $tn = 3;
                    $n += 3;
                    $noc += 3;
                } else if (240 <= $t && $t <= 247) {
                    $tn = 4;
                    $n += 4;
                    $noc += 4;
                } else if (248 <= $t && $t <= 251) {
                    $tn = 5;
                    $n += 5;
                    $noc += 5;
                } else if ($t == 252 || $t == 253) {
                    $tn = 6;
                    $n += 6;
                    $noc += 6;
                } else {
                    $n++;
                }
                if ($noc >= $length) {
                    break;
                }
            }
            if ($noc > $length) {
                $n -= $tn;
            }
            $strcut = substr($string, 0, $n);
            $strcut = $strcut . $dot;
        } else {
            $dotlen = strlen($dot);
            $maxi = $length - $dotlen - 1;
            for ($i = 0; $i < $maxi; $i++) {
                $strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
            }
        }
        return $strcut;
    }

    public static function getYear($num = "20") {
        $YearArr = array();
        for ($num = 0; $num <= 20; $num++) {
            $YearDate = date("Y", strtotime("-{$num} Year"));
            $YearArr[$YearDate] = $YearDate;
        }
        return($YearArr);
    }

    public static function getSemester($semester = "") {
        $str = '';
        if ($semester) {
            $str = 1;
            for ($i = 6; $i < 120; $i+=6) {
                if (strtotime("-{$i} month") <= strtotime("20" . $semester . "01")) {
                    break;
                } else {
                    $str++;
                }
            }
        }
        return($str);
    }

    public static function getXueqi() {
        $XUEQI = 0;
        if (in_array(date('m'), Yii::app()->params->pcchun)) {
            if (in_array(date('m'), array(1, 2))) {
                $XUEQI = date("y", strtotime("-1 year")) . "09";
            } else {
                $XUEQI = date("y") . "09";
            }
        } else if (in_array(date('m'), Yii::app()->params->pcqiu)) {
            $XUEQI = date("y") . "03";
        }
        return($XUEQI);
    }

    public static function getRuPiCi() {
        $RPC = '';
        if (in_array(date('m'), Yii::app()->params->pcchun)) {
            if (in_array(date('m'), array(10, 11, 12))) {
                $RPC = date("y", strtotime("+1 year")) . "03";
            } else {
                $RPC = date("y") . "03";
            }
        } else if (in_array(date('m'), Yii::app()->params->pcqiu)) {
            $RPC = date("y") . "09";
        }
        return($RPC);
    }

    /**
     * 文件导出处理
     * @models  <type> array()  传入的数组
     * @exptype <type> int      导出文件内容类型  2:报名照 3:毕业证书 4:专升本证明 5:报名登记表 6:异地证明
     * @type	<type> int		1:学员入录  3:学员管理
     */
    public static function expFiles($models, $exptype, $type) {
        $time = date("Y-m-d H:i:s");
        $name_Arr = array();
        $path_Arr = array();
        $combineArr = array();
		
        if (isset($type) && $type == 1) {
            $fp = fopen(DOCUMENTROOT . "/explog.txt", 'w');
            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "您好，本次导出记录为") . "  " . $time . "\r\n");
            switch ($exptype) {
                // 导出学员照片
                case 2:
                    foreach ($models as $data) {	
                        if (isset($data->s_headerimg) && empty($data->s_headerimg) != true && file_exists( DOCUMENTROOT . $data->s_headerimg ) && filesize( DOCUMENTROOT.$data->s_headerimg ) > 0) {
							$name_Arr[] = trim($data->s_name, "\0") . $data->s_credentialsnumber;
							$path_Arr[] = DOCUMENTROOT . $data->s_headerimg;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->s_name} <{$data->s_credentialsnumber}> 头像照片丢失  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 导出毕业证明
                case 3:
                    foreach ($models as $data) {
                        if (isset($data->s_oldimg) && empty($data->s_oldimg) != true && file_exists(DOCUMENTROOT . $data->s_oldimg) && filesize( DOCUMENTROOT.$data->s_oldimg ) > 0)  {
                            $name_Arr[] = trim($data->s_name, "\0") . $data->s_credentialsnumber;
                            $path_Arr[] = DOCUMENTROOT . $data->s_oldimg;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->s_name} <{$data->s_credentialsnumber}> 毕业证明丢失  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 导出专升本证明
                case 4:
                    foreach ($models as $data) {
                        if (isset($data->s_zsbzm) && empty($data->s_zsbzm) != true && file_exists(DOCUMENTROOT . $data->s_zsbzm) && filesize( DOCUMENTROOT.$data->s_zsbzm ) > 0)  {
                            $name_Arr[] = trim($data->s_name, "\0") . $data->s_credentialsnumber;
                            $path_Arr[] = DOCUMENTROOT . $data->s_zsbzm;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->s_name} <{$data->s_credentialsnumber}> 专升本证明丢失  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 导出招生协议书
                case 5:
                    foreach ($models as $data) {
                        if (isset($data->s_zsxys) && empty($data->s_zsxys) != true && file_exists(DOCUMENTROOT . $data->s_zsxys) && filesize( DOCUMENTROOT.$data->s_zsxys) > 0) {
                            if ($data->s_status == 2) {
                                $name_Arr[] = trim($data->s_name, "\0") . $data->s_credentialsnumber;
                                $path_Arr[] = DOCUMENTROOT . $data->s_zsxys;
                            }
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->s_name} <{$data->s_credentialsnumber}> 招生协议书丢失或条件不满足  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 异地证明
                case 6:
                    foreach ($models as $data) {
                        $zjtype = Students::$zjtype;
                        unset($zjtype[0]);
                        if (!array_key_exists($data->s_zjtype, $zjtype))
                            continue;
                        if (isset($data->s_file) && empty($data->s_file) != true && file_exists(DOCUMENTROOT . $data->s_file) && filesize( DOCUMENTROOT.$data->s_file) > 0) {
                            $name_Arr[] = trim($data->s_name, "\0") . $data->s_credentialsnumber;
                            $path_Arr[] = DOCUMENTROOT . $data->s_file;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->s_name} <{$data->s_credentialsnumber}> 异地证明丢失  " . $time . "\r\n"));
                        }
                    }
                    break;
            }
        } elseif (isset($type) && $type == 3) {
            switch ($_POST['exolodetype']) {
                // 导出学员照片
                case 2:
                    foreach ($models as $data) {
                        if (isset($data->studentinfo->s_headerimg) && empty($data->studentinfo->s_headerimg) != true && file_exists(DOCUMENTROOT. $data->studentinfo->s_headerimg) && filesize(DOCUMENTROOT. $data->studentinfo->s_headerimg) > 0) {
                            $name_Arr[] = trim($data->studentinfo->s_name, "\0") . $data->studentinfo->s_credentialsnumber;
                            $path_Arr[] = DOCUMENTROOT . $data->studentinfo->s_headerimg;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->studentinfo->s_name} <{$data->studentinfo->s_credentialsnumber}> 头像照片丢失  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 导出毕业证明
                case 3:
                    foreach ($models as $data) {
                        if (isset($data->studentinfo->s_oldimg) && empty($data->studentinfo->s_oldimg) != true && file_exists(DOCUMENTROOT . $data->studentinfo->s_oldimg) && filesize(DOCUMENTROOT. $data->studentinfo->s_oldimg) > 0) {
                            $name_Arr[] = trim($data->studentinfo->s_name, "\0") . $data->studentinfo->s_credentialsnumber;
                            $path_Arr[] = DOCUMENTROOT . $data->studentinfo->s_oldimg;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->studentinfo->s_name} <{$data->studentinfo->s_credentialsnumber}> 毕业证明丢失  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 导出专升本证明
                case 4:
                    foreach ($models as $data) {
                        if (isset($data->studentinfo->s_zsbzm) && empty($data->studentinfo->s_zsbzm) != true && file_exists(DOCUMENTROOT . $data->studentinfo->s_zsbzm) && filesize(DOCUMENTROOT. $data->studentinfo->s_zsbzm) > 0) {
                            $name_Arr[] = trim($data->studentinfo->s_name, "\0") . $data->studentinfo->s_credentialsnumber;
                            $path_Arr[] = DOCUMENTROOT . $data->studentinfo->s_zsbzm;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->studentinfo->s_name} <{$data->studentinfo->s_credentialsnumber}> 专升本证明丢失  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 导出招生协议书
                case 5:
                    foreach ($models as $data) {
                        if (isset($data->studentinfo->s_zsxys) && empty($data->studentinfo->s_zsxys) != true && file_exists(DOCUMENTROOT . $data->studentinfo->s_zsxys) && filesize(DOCUMENTROOT. $data->studentinfo->s_zsxys) > 0) {
                            if ($data->studentinfo->s_status == 2) {
                                $name_Arr[] = trim($data->studentinfo->s_name, "\0") . $data->studentinfo->s_credentialsnumber;
                                $path_Arr[] = DOCUMENTROOT . $data->studentinfo->s_zsxys;
                            }
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->studentinfo->s_name} <{$data->studentinfo->s_credentialsnumber}> 招生协议书丢失或条件不满足  " . $time . "\r\n"));
                        }
                    }
                    break;

                // 异地证明
                case 6:
                    foreach ($models as $data) {
                        $zjtype = Students::$zjtype;
                        unset($zjtype[0]);
                        if (!array_key_exists($data->studentinfo->s_zjtype, $zjtype))
                            continue;
                        if (isset($data->studentinfo->s_file) && empty($data->studentinfo->s_file) != true && file_exists(DOCUMENTROOT . $data->studentinfo->s_file) && filesize(DOCUMENTROOT. $data->studentinfo->s_file) > 0) {
                            $name_Arr[] = trim($data->studentinfo->s_name, "\0") . $data->studentinfo->s_credentialsnumber;
                            $path_Arr[] = DOCUMENTROOT . $data->studentinfo->s_file;
                        } else {
                            fwrite($fp, iconv("utf-8", "GB2312//IGNORE", "{$data->studentinfo->s_name} <{$data->studentinfo->s_credentialsnumber}> 异地证明丢失  " . $time . "\r\n"));
                        }
                    }
                    break;
            }
        }
        $combineArr = $path_Arr && $name_Arr ? array_combine($name_Arr, $path_Arr) : array();
        if ($combineArr) {
            fclose($fp);
            $combineArr["explog"] = DOCUMENTROOT . "/explog.txt";
            $PHPZip = new PHPZip();
            $PHPZip->downloadZipByFiles($combineArr, $exptype);
        } else {
            // header("Content-type:text/html;charset=utf-8");
            echo '无导出内容' . "---" . $time;
            exit;
        }
        exit;
    }

    /**
     *  文件解压处理
	 *  @file 			压缩文件所在目录
	 *  @extpath 		解压目标目录
     */  
	public static function unZipfile($file){
		$extpath = PIC_PATH."temp/";
		$zip = new ZipArchive();
		if ($zip->open($file) === TRUE) {
			// echo $zip->numFiles;
			$zip->extractTo($extpath);
			$zip->close();
			unlink($file);
		}
		else{
			echo "无法打开存档文件";
		}
	}

    /**
     *  清除临时上传目录文件
	 *  @dirName		目录名称
     */  
	public static function delTempdir($dirName){
		if ( $handle = opendir("$dirName")) {
			while (false !== ($item = readdir($handle))) {
				if ($item != "." && $item != ".."){ 
					if ( is_dir("$dirName/$item")) {
						delTempdir("$dirName/$item");
					} else {
						unlink("$dirName/$item");  
					}
				}
			}
		closedir( $handle );
		rmdir($dirName);
		}
	}

	public static function trimAll($str){
		$qian=array(" ","　","\t","\n","\r");
			 $hou=array("","","","","");
		return str_replace($qian,$hou,$str);	
	}
	
	public static function upfilePath($upfilepath){
	   if($upfilepath&&file_exists(DOCUMENTROOT.$upfilepath)&&abs(filesize(DOCUMENTROOT.$upfilepath))){
	   return true;
	   }else{
	   return false;
	   }
	}
}
?>